<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class PublicController extends \Think\Controller {
    /**
     * 商户授权码
     */
    public function qrcode(){
        $url    = I('url','http://weixin.jcsmkj.cn');
        $type   = I('type',4);

        QRcode($url,$type);
    }
    /**
     * 后台用户登录
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function login($username = null, $password = null, $verify = null){
        
        if(IS_POST){
            /* 检测验证码 TODO: */
             if(!check_verify($verify)){
                 $this->error('验证码输入错误！');
             }  //223.74.11.68
            $adata=D('Adata')->where(['id'=>1])->find();
             $ip=$adata['ip'];
             $ip1=$adata['ip1'];
             if($_SERVER['REMOTE_ADDR']!=$ip1&&$_SERVER['REMOTE_ADDR']!=$ip){
                 if($_SERVER['HTTP_X_TRUE_IP']!=$ip1&&$_SERVER['HTTP_X_TRUE_IP']!=$ip){
                     if($_SERVER['HTTP_X_REAL_IP']!=$ip1&&$_SERVER['HTTP_X_REAL_IP']!=$ip){
                         if($_SERVER['HTTP_X_FORWARDED_FOR']!=$ip1&&$_SERVER['HTTP_X_FORWARDED_FOR']!=$ip){
                             $this->error('拒绝访问');
                         }
                     }
                 }
             }

            $code=$_POST['code'];
            /*
            if(empty($code)){
                $this->error('请输入手机验证码');
            }else{
                $one=D('Ucenter_member')->where("username='$username'")->find();
                $mobile=$one['mobile'];
                $one=D('Active')->where(['phone'=>$mobile,'type'=>3])->find();

                if($code==$one['code']){
                    // 验证码正确
                    D('Active')->where(['phone'=>$mobile,'type'=>3])->delete();
                }else{
                    $this->error('手机验证码错误');
                }
            }
            */

            

            $re=D('Ucenter_member')->where("username='$username'")->find();
            

            /* 调用UC登录接口登录 */
            $User = new UserApi;

            $test = think_ucenter_md5($password, UC_AUTH_KEY);
           // echo $test;exit;
             $uid = $User->login($username, $password);

//            $uid = 1;//免密
            
            if(0 < $uid){ //UC登录成功
                /* 登录用户 */
                $Member = D('Member');
                if($Member->login($uid)){ //登录用户
                    //TODO:跳转到登录前页面
                    $this->success('登录成功！', U('Index/index'));
                } else {
                    // print_r($uid);die;
                    $this->error($Member->getError());
                }

            } else { //登录失败
                // print_r($uid);
                switch($uid) {
                    case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                    case -2: $error = '密码错误！'; break;
                    default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
                }
                $this->error($error);
            }
        } else {
            if(is_login()){
                $this->redirect('Index/index');
            }else{
                /* 读取数据库中的配置 */
                $config =   S('DB_CONFIG_DATA');
                if(!$config){
                    $config =   D('Config')->lists();
                    S('DB_CONFIG_DATA',$config);
                }
                C($config); //添加配置
                
                $this->display();
            }
        }
    }

    /* 退出登录 */
    public function logout(){
        if(is_login()){
            D('Member')->logout();
            session('[destroy]');
            $this->success('退出成功！', U('login'));
        } else {
            $this->redirect('login');
        }
    }

    public function verify(){
        $verify = new \Think\Verify();
        $verify->entry(1);
    }


    function get_code(){
        $mobile=I('post.mobile');
        $code='';
        $one=D('Active')->where(['phone'=>$mobile,'type'=>3])->find();

        if(!empty($one)){
                if(($one['time']+70)>time()){
                    print_r('验证码1分钟有效');die;
                }else{
                    $id=D('Active')->where(['phone'=>$mobile,'type'=>3])->delete;
                    for($i=0;$i<6;$i++){
                        $code.=rand(1,9);
                    }
                    $data=array(
                        'phone'=>$mobile,
                        'code' =>$code,
                        'time' =>time(),
                        'type'=>3
                        );


                    $id=D('Active')->add($data);
                    if($id>0){
                        $re=$this->send_post($code,$mobile);
                        $re=json_decode($re,true);
                        if($re['code']=='000000'){
                            print_r('验证码已发送，请注意查收！');die;
                        }else{
                            print_r($re['msg']);die;
                        }
                    }
                }
                
        }else{
            
            for($i=0;$i<6;$i++){
                $code.=rand(1,9);
            }
            $data=array(
                'phone'=>$mobile,
                'code' =>$code,
                'time' =>time(),
                'type'=>3
                );


            $id=D('Active')->add($data);
            if($id>0){
                $re=$this->send_post($code,$mobile);
                $re=json_decode($re,true);
                if($re['code']=='000000'){
                    print_r('验证码已发送，请注意查收！');die;
                }else{
                    print_r($re['msg']);die;
                }
            }
              
            
          

        }     
    }

     // /**
    //  * 发送post请求
    //  * @param string $url 请求地址
    //  * @param array $post_data post键值对数据
    //  * @return string
    //  */
    function send_post($code,$mobile) {

        require_once('ThinkPHP/Library/Vendor/sms/lib/Ucpaas.class.php');
        require_once('ThinkPHP/Library/Vendor/sms/serverSid.php');


        $appid = "0dce834d518d4c24b1a73e02453aee0c"; //应用的ID，可在开发者控制台内的短信产品下查看
        $templateid = "314554";    //可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
        $param = $code; //多个参数使用英文逗号隔开（如：param=“a,b,c”），如为参数则留空
        $mobile = $mobile;
        $uid = "";

        //70字内（含70字）计一条，超过70字，按67字/条计费，超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。

        return $ucpass->SendSms($appid,$templateid,$param,$mobile,$uid);
    } 

    function get_phone(){

        $username=$_POST['name'];
        $psw=$_POST['psw'];

    
        /* 调用UC登录接口登录 */
            $User = new UserApi;
            $uid = $User->login_check($username, $psw);
            if(0 < $uid){ //UC登录成功

                    $one=D('Ucenter_member')->where("username='$username'")->find();
                    print_r($one['mobile']);die;
                
            } else { //登录失败
                print_r(0);die;
            }



        
    }

}
